﻿package com.saint.bitmapDistortion.animations 
{
	import caurina.transitions.Tweener;
	import com.saint.bitmapDistortion.StretchyImage;
	import com.saint.utils.Logger;
	import flash.geom.Point;
	
	
	
	/**
	 * animates a stretchy image as if it is sucked into the bottom of the screen in the middle
	 * 
	 * 	made for images of 2x2 tiles
	 * 
	 * 
	 * @author kembera
	 */
	public class SuckbottomMiddle implements IStretchyAnimation
	{
		private var _image:StretchyImage;
		private var animationPoints:Array = new Array();
		
		//private var transition:String = "easeInQuint"
		private var transition:String = "easeInCubic"
		
		
		/**
		 * CONTRUCTOR
		 * 
		 * @param	image
		 */
		public function SuckbottomMiddle(image:StretchyImage) 
		{
			Logger.info("SuckbottomMiddle.SuckbottomMiddle");	
			_image = image;
			animationPoints = getAnimationPoints();
		}
		
		/**
		 * check the image has the correct number of tiles
		 */
		private function checkImageFormat():void
		{
			Logger.info("SuckBottomMddle.checkImageFormat");	
		}
		
		/**
		 * GETS THE POINTS
		 */
		private function getAnimationPoints():Array
		{
			Logger.info("SuckBottomMddle.getAnimationPoints");
			
			/* check the image has the correct rows and colums */
			var rows:uint = _image.rows;
			var cols:uint = _image.cols;			
			if (!(rows == 2 && cols == 2))
			{
				throw new Error("SuckBottomMddle.getAnimationPoints ANIMATION ONLY WORK FOR IMAGES WITH 2X2 ROWS");
			}
			
			Logger.debug("SuckBottomMddle.getAnimationPoints rows: " + rows + ", cosl: " + cols )
			
			var points:Array = _image.buttons;
			return points;
			
		}
		public function startAnimation():void
		{
			Logger.info("SuckbottomMiddle.startAnimation");
			
			var stageWidth:Number = _image.stage.stageWidth;
			var stageHeight:Number = _image.stage.stageHeight;
			
			var destinationPoint = new Point(stageWidth / 2, stageHeight + 10);
			
			/* bottom middle */
			Tweener.addTween(animationPoints[7], { time:1, y: destinationPoint.y,x:destinationPoint.x, transition:transition } );
			
			/* bottom sides */
			Tweener.addTween(animationPoints[6], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.2, transition:transition } );
			Tweener.addTween(animationPoints[8], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.2, transition:transition } );
			
			/** middle middle */
			Tweener.addTween(animationPoints[4], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.25, transition:transition } );
			/** middle sides */
			Tweener.addTween(animationPoints[3], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.3, transition:transition } );
			Tweener.addTween(animationPoints[5], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.3, transition:transition } );
			
			/** top middle */
			Tweener.addTween(animationPoints[1], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.35, transition:transition } );
			/** top sides */
			Tweener.addTween(animationPoints[0], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.36, transition:transition } );
			Tweener.addTween(animationPoints[2], { time:1, y: destinationPoint.y, x:destinationPoint.x, delay: 0.36, transition:transition } );
			
		}
		
		
	}

}